package org.example.c_0_004;

import org.junit.Test;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * LockSupport 控制线程执行
 */
public class T_LockSupport {

    @Test
    public void test(){
        Thread t = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    System.out.println(i);
                    TimeUnit.SECONDS.sleep(1);
                    if (i==5) {
                        LockSupport.park();// 暂停执行
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t");

        t.start();

        LockSupport.unpark(t);// t继续执行，且该命令可以提前执行

        try {
            t.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
